﻿
<?php 
include "fonctions.php";header('Content-Type: text/html; charset=utf-8');head_html("Brouillon");
if(isset($_SESSION['idUtil']))
{ 
    db_connect();
	en_tete();
	echo '<script type="text/javascript" src="./tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';
	
	echo '<script type="text/javascript">tinyMCE.init({theme : "advanced",
			mode : "textareas",
			plugins : "autolink,lists,pagebreak,style,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,outdent,indent,blockquote,|,undo,redo,|,insertdate,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "sub,sup,|,charmap,emotions,|,print,|,ltr,rtl,|,visualchars,nonbreaking,pagebreak,restoredraft,|,fullscreen",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_path : false});</script>';
			
	echo '<script type="text/javascript" src="./ajax.js"> </script>';
	
	echo '<div id="contenu">';
	menu_lateral_droit();
	echo '<div id="texte">';		vousetesici("Brouillon");
		if(isset($_POST['idBrouillon']))
		{
			$req='SELECT * FROM articles where idArticle=\''.$_POST['idBrouillon'].'\';';
			$req=mysql_query($req) or die ('Erreur select :'.mysql_error());
			$article = mysql_fetch_array($req);
		}
	echo "<h1>R&eacute;diger un texte libre</h1></br></br></br>";
	if(!(isset($_POST['idBrouillon'])) or (isset($_POST['idBrouillon']) and $article['etat']=='brouillon'))
	{
		echo '<form method="post" id="form_brouillon">
			<p> 
				Titre : <input type="text" name="titre" id="titre" size="50" maxlength="50"';
				if(isset($_POST['idBrouillon']))
				{
					echo 'value="'.$article['titre'].'"';
				}
				echo '/></br></br>
				<textarea name="text" id="text" cols="80" rows="20">';
				if (isset($_POST['idBrouillon']))
				{
					$fic=fopen($article['path'],"r");
					while (!feof($fic)) {
					echo fgets($fic, 4096); // lecture du contenu de la ligne
					}
				}
				echo '</textarea>
				<input type="button" name="enregistrer" id="enregistrer" value="Enregistrer" onClick="enregistrer_brouillon(\'brouillon\')" />
				<input type="button" name="valider" id="valider" value="Faire valider" onClick="enregistrer_brouillon(\'en attente\')"/>
				Atelier :<select id="atelier" ';
				if(isset($_POST['idSujet']))
				{
					echo 'disabled="disabled"';
				}
				echo '>';
				  $req='SELECT nomAtelier FROM inscriptions where pseudo=\''.$_SESSION['idUtil'].'\';';
			      $req=mysql_query($req) or die ('Erreur select :'.mysql_error());
			      while($atelier = mysql_fetch_array($req))
				  {
					$req='SELECT idSujet FROM sujet WHERE nomAtelier = \''.$atelier['nomAtelier'].'\' AND texte=\'Libre\';';
					$res  = mysql_query($req) or die (mysql_error());
				    $sujet = mysql_fetch_array($res);
				    $sujet = $sujet['idSujet'];
				    echo '<option value="'.$sujet.'"';
					if(isset($_POST['idSujet']) and $_POST['idSujet']==$sujet)
					{
					  echo 'selected="selected"';
					}
					else{
						if((!(isset($_POST['idBrouillon'])) and $sujet==1) or (isset($_POST['idBrouillon']) and $article['idSujet']==$sujet)){
						 echo 'selected="selected"';
						}
					}
					echo '>'.$atelier['nomAtelier'].'</option>';
				  }
				echo '</select>
				<input type="hidden" name="id_brouillon" id="id_brouillon"';
				if(isset($_POST['idBrouillon']))
				{
					echo 'value="'.$_POST['idBrouillon'].'"';
				}else{
					echo 'value="new"';
				}
				echo '/>
			</p>
		</form>';
	}else{
	  echo 'l\'article que vous avez selectioné n\'est pas un brouillon il ne peut pas être édité';
	}
	echo "</div>";
	pied_page();
	echo "</div>";
}
?>
